package _11_整理题目._8_多线程;

/**
 * https://blog.csdn.net/WaterTheGreat/article/details/79072950
 * 三个线程顺序打印 ABC ，重复 5 次
 */
public class PrintThree {
    
    private static boolean flagA = true;
    private static boolean flagB = false;
    private static boolean flagC = false;
    
    public static void main(String[] args) {
        Object lock = new Object();
        
        Thread aThread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; ) {
                    synchronized (lock) {
                        if (flagA) {
                            //线程A执行
                            System.out.printf("A");
                            flagA = false;
                            flagB = true;
                            flagC = false;
                            lock.notifyAll();
                            i++;
                        } else {
                            try {
                                lock.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
    
        Thread bThread = new Thread(new Runnable() {
        
            @Override
            public void run() {
                for (int i = 0; i < 5; ) {
                    synchronized (lock) {
                        if (flagB) {
                            //线程B执行
                            System.out.printf("B");
                            flagA = false;
                            flagB = false;
                            flagC = true;
                            lock.notifyAll();
                            i++;
                        } else {
                            try {
                                lock.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
    
        Thread cThread = new Thread(new Runnable() {
        
            @Override
            public void run() {
                for (int i = 0; i < 5; ) {
                    synchronized (lock) {
                        if (flagC) {
                            //线程C执行
                            System.out.println("C");
                            flagA = true;
                            flagB = false;
                            flagC = false;
                            lock.notifyAll();
                            i++;
                        } else {
                            try {
                                lock.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        
        aThread.start();
        bThread.start();
        cThread.start();
    }
}

